﻿using SharPy.WebObjects;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;

namespace SharPy.DataModel
{
    /// <summary>
    /// Универсальная модель данных элементов.
    /// </summary>
    public class DataItem
    {
        public DataItem(String uniqueId, String title, String subtitle, String imagePath, String description, String content)
        {
            this.UniqueId = uniqueId;
            this.Title = title;
            this.Subtitle = subtitle;
            this.Description = description;
            this.ImagePath = imagePath;
            this.Content = content;
        }

        public string UniqueId { get; private set; }
        public string Title { get; private set; }
        public string Subtitle { get; private set; }
        public string Description { get; private set; }
        public string ImagePath { get; private set; }
        public string Content { get; private set; }

        public override string ToString()
        {
            return this.Title;
        }
    }

    /// <summary>
    /// Универсальная модель данных групп.
    /// </summary>
    public class DataGroup
    {
        public DataGroup(String uniqueId, String title, String subtitle, String imagePath, String description)
        {
            this.UniqueId = uniqueId;
            this.Title = title;
            this.Subtitle = subtitle;
            this.Description = description;
            this.ImagePath = imagePath;
            this.Items = new ObservableCollection<DataItem>();
        }

        public string UniqueId { get; private set; }
        public string Title { get; private set; }
        public string Subtitle { get; private set; }
        public string Description { get; private set; }
        public string ImagePath { get; private set; }
        public ObservableCollection<DataItem> Items { get; private set; }

        public override string ToString()
        {
            return this.Title;
        }
    }

    /// <summary>
    /// </summary>
    public sealed class DataSource
    {
        private static DataSource _DataSource = new DataSource();

        private ObservableCollection<DataGroup> _groups = new ObservableCollection<DataGroup>();
        public ObservableCollection<DataGroup> Groups
        {
            get { return this._groups; }
        }

        public static async Task<IEnumerable<DataGroup>> GetGroupsAsync()
        {
            await _DataSource.GetDataAsync();

            return _DataSource.Groups;
        }

        public static async Task<DataGroup> GetGroupAsync(string uniqueId)
        {
            await _DataSource.GetDataAsync();
            // Для небольших наборов данных можно использовать простой линейный поиск
            var matches = _DataSource.Groups.Where((group) => group.UniqueId.Equals(uniqueId));
            if (matches.Count() == 1) return matches.First();
            return null;
        }

        public static async Task<DataItem> GetItemAsync(string uniqueId)
        {
            await _DataSource.GetDataAsync();
            // Для небольших наборов данных можно использовать простой линейный поиск
            var matches = _DataSource.Groups.SelectMany(group => group.Items).Where((item) => item.UniqueId.Equals(uniqueId));
            if (matches.Count() == 1) return matches.First();
            return null;
        }

        private async Task GetDataAsync()
        {
            if (this._groups.Count != 0)
                return;
            try
            {
                StorageFile file = await StorageFile.GetFileFromPathAsync(SharPy.GameSettings.clSettings.getGameList()); //await StorageFile.GetFileFromApplicationUriAsync(dataUri);
                string xmlText = await FileIO.ReadTextAsync(file);

                clXmlHandler xmlHandler = new clXmlHandler();

                List<clGameArray> xmlArray = new List<clGameArray>();
                xmlArray.Add(new clGameArray(await xmlHandler.getGameInfo(await xmlHandler.getXmlFromFile(file))));

                foreach (clGameArray groupValue in xmlArray)
                {
                    DataGroup group = new DataGroup(groupValue.uniqueID, groupValue.sTitle, groupValue.sSubtitle, groupValue.sImagePath, groupValue.sDescription);

                    foreach (clGameInfo itemValue in groupValue.Items)
                    {
                        group.Items.Add(new DataItem(itemValue.uniqueID, itemValue.sName, itemValue.sDescription, null, itemValue.sDescription, itemValue.sURL));
                    }
                    this.Groups.Add(group);
                }
            }
            catch (System.IO.FileNotFoundException)
            {
            }
            catch (System.Xml.XmlException)
            {
            }
            catch (Exception e)
            {
                //First launch probably
            }
        }
    }
}